//
//  ReplyViewController.m
//  Feeds2
//
//  Created by cos on 2020/12/2.
//  Copyright © 2020 cos. All rights reserved.
//

#import "ReplyViewController.h"
#import "AFNetworking.h"
#import "Single.h"
#import "commentCell.h"
#import "commentCell2.h"
@interface ReplyViewController ()

@end

@implementation ReplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[Single sharedInstance].commentlist2 removeAllObjects];
    self.commentlist = [[NSMutableArray alloc]init];
    CGRect screenFrame = [UIScreen mainScreen].bounds;
    int screenWidth = screenFrame.size.width;
    int screenHeight = screenFrame.size.height;
    //comment
    /*NSString *url = @"http://172.18.178.56/api/comment/";
    
    NSString *contentID = [Single sharedInstance].replyid;
    url = [url stringByAppendingFormat:@"%@", contentID];
    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSDictionary *dict2 = (NSDictionary *)dict[@"Data"];
        
        //NSLog(@"%@",dict2);
        [[Single sharedInstance].commentlist2 removeAllObjects];
        for(id key in dict2){
            NSDictionary *temp = [[NSDictionary alloc]init];
            NSString *lnum = [NSString stringWithFormat:@"%@",key[@"Comment"][@"LikeNum"]];
            temp = @{@"Content":key[@"Comment"][@"Content"],
                     @"ID":key[@"Comment"][@"ID"],
                     //@"LikeNum":lnum,
                     @"LikeNum":key[@"Comment"][@"LikeNum"],
                     @"UserName":key[@"User"][@"Name"],
                     @"FatherID":key[@"Comment"][@"FatherID"],
                     @"UserID":key[@"Comment"][@"UserID"]
            };
            [[Single sharedInstance].commentlist2 addObject:temp];
            //[self.commentlist addObject:temp];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //
    }];*/
    //comment
    //comment
    NSDictionary *dict = (NSDictionary *)[Single sharedInstance].replylist;
    for(id key in dict){
        NSDictionary *temp = [[NSDictionary alloc]init];
        temp = @{@"Content":key[@"Reply"][@"Content"],
                 @"ID":key[@"Reply"][@"ID"],
                 //@"LikeNum":lnum,
                 @"LikeNum":key[@"Reply"][@"LikeNum"],
                 @"UserName":key[@"User"][@"Name"],
                 @"FatherID":key[@"Reply"][@"FatherID"],
                 @"UserID":key[@"Reply"][@"UserID"],
                 @"Date":key[@"Reply"][@"Date"]
        };
        [[Single sharedInstance].commentlist2 addObject:temp];
        //[self.commentlist addObject:temp];
    }
    //comment
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 100, screenWidth, screenHeight) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [Single sharedInstance].commentlist2.count;
    //return self.commentlist.count;
}

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView* v = [[UIView alloc]init];
    v.backgroundColor = [UIColor clearColor];
    return v;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView* v = [[UIView alloc]init];
    v.backgroundColor = [UIColor clearColor];
    return v;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height + 30;
     //return 90;
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"cell";
    commentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone; //禁止点击
    if (cell == nil) {
        cell = [[commentCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSString *lnum = [NSString stringWithFormat:@"%@",[Single sharedInstance].commentlist2[indexPath.section][@"LikeNum"]];
    NSString *dat = [NSString stringWithFormat:@"%@",[Single sharedInstance].commentlist2[indexPath.section][@"Date"]];
    long long  myInt = [dat longLongValue];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(myInt/1000+57600)];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    NSString *nam = [NSString stringWithFormat:@"%@",[Single sharedInstance].commentlist2[indexPath.section][@"UserName"]];
    [cell setLabel1Text:[Single sharedInstance].commentlist2[indexPath.section][@"Content"]
             label2Text:lnum
             label3Text:confromTimespStr
             label4Text:nam];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end
